package com.mask.im.business.strategy.impl.register;

import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mask.im.business.model.entity.UserAuth;
import com.mask.im.business.model.entity.UserInfo;
import com.mask.im.business.model.entity.UserRole;
import com.mask.im.business.enums.SignUpNoticeEnum;
import com.mask.im.business.common.exception.BusinessException;
import com.mask.im.business.mapper.UserAuthMapper;
import com.mask.im.business.mapper.UserInfoMapper;
import com.mask.im.business.mapper.UserRoleMapper;
import com.mask.im.business.model.vo.RegisterVO;
import com.mask.im.business.service.RedisService;
import com.mask.im.business.strategy.RegisterStrategy;
import com.mask.im.business.common.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

import static com.mask.im.business.common.constant.RedisConstant.*;
import static com.mask.im.business.common.constant.RegisterConstant.*;
import static com.mask.im.business.common.constant.RegisterConstant.DEFAULT_ROLE;


@Service("phoneRegisterImpl")
public class PhoneRegisterImpl implements RegisterStrategy {

	@Autowired
	private UserAuthMapper userAuthMapper;

	@Autowired
	private UserInfoMapper userInfoMapper;

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Autowired
	private RedisService redisService;

	@Autowired
	private UserRoleMapper userRoleMapper;

	@Autowired
	private CommonUtil commonUtil;

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void register(RegisterVO registerVO) {
		verify(registerVO);
		Integer count = userAuthMapper.selectCount(new LambdaQueryWrapper<UserAuth>()
				.eq(UserAuth::getPhone, registerVO.getPhone()));
		if (count>0){
			throw new BusinessException("手机号已经被其他账号绑定");
		}
		UserInfo userInfo = UserInfo.builder()
				.username(registerVO.getPhone())
				.avatar(DEFAULT_AVATAR)
				.nickName("用户" + System.currentTimeMillis())
				.gender(DEFAULT_GENDER)
				.disable(DEFAULT_DISABLE).build();
		userInfoMapper.insert(userInfo);
		//保存用户账户信息
		UserAuth auth = UserAuth.builder()
				.userInfoId(userInfo.getId())
				.phone(registerVO.getPhone())
				.password(passwordEncoder.encode(DEFAULT_PASSWORD))
				.loginType(EMAIL_TYPE)
				.username(registerVO.getPhone())
				.build();
		userAuthMapper.insert(auth);
		//保存用户角色信息
		UserRole userRole = UserRole.builder()
				.userInfoId(userInfo.getId())
				.roleId(DEFAULT_ROLE).build();
		userRoleMapper.insert(userRole);
		redisService.del(PHONE_REGISTER + registerVO.getPhone());
		commonUtil.signUpNotifications(userInfo.getUsername(), SignUpNoticeEnum.getSignUpNoticeType(PHONE_TYPE));
	}

	public void verify(RegisterVO registerVO){
		if (!Validator.isMobile(registerVO.getPhone())) {
			throw new BusinessException("手机号格式错误");
		}
		String code = (String) redisService.get(PHONE_REGISTER + registerVO.getPhone());
		if (Objects.isNull(code)) {
			throw new BusinessException("验证码已过期");
		}
		if (registerVO.getCode() == null || !code.equals(registerVO.getCode())) {
			throw new BusinessException("验证码不正确");
		}
	}
}
